%%%-------------------------------------------------------------------
%%% @author 92913
%%% @copyright (C) 2024, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 20. 7月 2024 14:27
%%%-------------------------------------------------------------------
-module(d18).
-author("92913").
%% 删出列表或元组中的所有重复元素  [1,2,2,3]
%% 先返回重复列表， 在删除
-export([my_member/2,find_duplicates/1,delete_duplicates/1]).

%% 第一步返回重复列表
find_duplicates(List) ->
  case is_list(List) of
    true -> find_duplicates(List, []);
    false -> find_duplicates(tuple_to_list(List), [])
end.

find_duplicates([], Result) -> Result; % 返回重复元素列表
find_duplicates([H | List], Result) ->
  Flag = my_member(H, List),
  if
    Flag == true -> Temp = [H | Result], find_duplicates(List, Temp);
    true -> find_duplicates(List, Result)
  end.

%% 开始
delete_duplicates(List) ->
  Dup = find_duplicates(List),
  if
    is_tuple(List) -> List2 = tuple_to_list(List),delete_duplicates(List2, Dup, []);
    true ->  delete_duplicates(List, Dup, []) %% 第二步 根据重复列表删除重复元素
  end.

delete_duplicates([H | List], Dup, Result) ->
  Bool = my_member(H, Dup),
  if
    Bool == true -> delete_duplicates(List, Dup, Result);
    true -> Temp = [H | Result], delete_duplicates(List, Dup, Temp)
  end;
delete_duplicates([], _, Result) -> Result.

%% 判断列表某个元素是否存在
my_member(E, [E | _]) ->
  true;
my_member(E, [_ | T]) ->
  my_member(E, T);
my_member(_, []) ->
  false.